package com.stormrage.edu.ctrl.learn;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.stormrage.edu.serv.edu.EduChapterServ;
import com.stormrage.edu.serv.edu.EduCourseServ;
import com.stormrage.edu.serv.edu.EduVideoServ;
import com.stormrage.edu.serv.learn.LeaStudentCourseServ;
import com.stormrage.edu.serv.learn.LeaStudentServ;
import com.stormrage.edu.serv.learn.LeaStudentVideoServ;
import com.stormrage.edu.serv.student.StuAcctServ;
import com.stormrage.entity.edu.EduVideo;
import com.stormrage.entity.edu.rep.EduChapterRep;
import com.stormrage.entity.edu.rep.EduVideoRep;
import com.stormrage.entity.learn.LeaStudent;
import com.stormrage.entity.learn.rep.LeaCourseStudyRep;
import com.stormrage.entity.learn.req.LeaCourseStudyReq;
import com.stormrage.entity.learn.req.LeaStudentReq;
import com.stormrage.entity.learn.req.LeaStudentVideoReq;
import com.stormrage.entity.learn.view.LeaStudentVideo;
import com.stormrage.entity.rep.ErrorRep;
import com.stormrage.entity.student.StuAcct;
import com.stormragetech.core.ctrl.BaseCtrl;

/**
 * @Title: LeaStudyCtrl.java
 * @Package com.stormrage.edu.ctrl.learn
 * @Description: 课程类型学习 Controller
 * @author www.stormragetech.com
 * @date 2016年5月11日
 * @version V1.0
 */
@Controller
@RequestMapping("/study/course")
public class LeaStudyCourseCtrl extends BaseCtrl {

	private static final Logger logger = LoggerFactory.getLogger(LeaStudyCourseCtrl.class);

	@Value("#{constants['html.title']}")
	private String htmlTitle;

	@Value("#{constants['html.logo']}")
	private String htmlLogo;

	@Value("#{constants['url.root']}")
	private String rootUrl;

	@Value("#{constants['path.image']}")
	private String imagePath;

	@Value("#{constants['path.thumb']}")
	private String thumbPath;

	@Value("#{constants['path.video']}")
	private String videoPath;

	@Autowired
	private EduChapterServ eduChapterServ;

	@Autowired
	private EduCourseServ eduCourseServ;

	@Autowired
	private EduVideoServ eduVideoServ;

	@Autowired
	private LeaStudentServ leaStudentServ;

	@Autowired
	private LeaStudentCourseServ leaStudentCourseServ;

	@Autowired
	private LeaStudentVideoServ leaStudentVideoServ;

	@Autowired
	private StuAcctServ stuAcctServ;

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public String course(@PathVariable("id") Integer id, HttpServletRequest request, Model model,
			RedirectAttributes redirect) throws UnsupportedEncodingException {

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 course 】-【 GET 】-【 STARTED 】");

		ErrorRep err = null;
		String errMsg = "";

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaCourseStudyReq req = new LeaCourseStudyReq(curr.getId(), id, 0);
			logger.debug(" caonima " + leaStudentCourseServ.checkSelect(req));
			if (!leaStudentCourseServ.checkSelect(req)) {
				errMsg = "您还未选课，请先选课";
				err = ErrorRep.instance(false, errMsg);
				redirect.addFlashAttribute("err", err);
				return "redirect:/select/{id}";
			}
			List<LeaCourseStudyRep> ness = eduCourseServ.queryStudyRepList(req);

			req = new LeaCourseStudyReq(curr.getId(), id, 1);
			List<LeaCourseStudyRep> selected = eduCourseServ.queryStudyRepList(req);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("user", curr);
			model.addAttribute("id", id);
			model.addAttribute("ness", ness);
			model.addAttribute("selected", selected);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 course 】-【 GET 】-【 COMPLETED 】");
		return "learn/course/course";
	}

	@RequestMapping(value = "/{aid}/{id}", method = RequestMethod.GET)
	public String chap(@PathVariable("aid") Integer aid, @PathVariable("id") Integer id, HttpServletRequest request,
			Model model) throws UnsupportedEncodingException {

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 chap 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq stuReq = new LeaStudentReq(curr.getId(), aid);
			LeaStudent stu = leaStudentServ.queryByAcctTerm(stuReq);

			LeaStudentVideoReq req = LeaStudentVideoReq.instance(stu.getId(), id, null);
			List<EduChapterRep> array = eduChapterServ.queryRepListByCourseId(req);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("tpath", rootUrl + thumbPath);
			model.addAttribute("user", curr);
			model.addAttribute("aid", aid);
			model.addAttribute("id", id);
			model.addAttribute("data", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 chap 】-【 GET 】-【 COMPLETED 】");
		return "learn/course/chap";
	}

	@RequestMapping(value = "/{aid}/{bid}/{cid}/{id}", method = RequestMethod.GET)
	public String video(@PathVariable("aid") Integer aid, @PathVariable("bid") Integer bid,
			@PathVariable("cid") Integer cid, @PathVariable("id") Integer id, HttpServletRequest request, Model model)
			throws UnsupportedEncodingException {

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 video 】-【 GET 】-【 STARTED 】");

		try {

			// ------------------ 获取用户信息 ------------------
			Subject currentUser = SecurityUtils.getSubject();
			StuAcct curr = stuAcctServ.queryByAcct(currentUser.getPrincipal().toString());

			// ------------------ Controller 操作 ------------------
			LeaStudentReq stuReq = new LeaStudentReq(curr.getId(), aid);
			LeaStudent stu = leaStudentServ.queryByAcctTerm(stuReq);

			EduVideo entity = eduVideoServ.queryById(id);
			LeaStudentVideoReq svReq = LeaStudentVideoReq.instance(stu.getId(), null, null, id, null);
			LeaStudentVideo stuVideo = leaStudentVideoServ.queryByStuVideo(svReq);

			LeaStudentVideoReq req = LeaStudentVideoReq.instance(stu.getId(), cid);
			List<EduVideoRep> array = eduVideoServ.queryRepListByChapId(req);

			// ------------------ Model 赋值 ------------------
			model.addAttribute("ipath", rootUrl + imagePath);
			model.addAttribute("tpath", rootUrl + thumbPath);
			model.addAttribute("vpath", rootUrl + videoPath);
			model.addAttribute("user", curr);
			model.addAttribute("aid", aid);
			model.addAttribute("bid", bid);
			model.addAttribute("prog", stuVideo);
			model.addAttribute("data", entity);
			model.addAttribute("array", array);

		} catch (Exception e) {
			model.addAttribute("err", e.getMessage());
			return "error/500";
		}

		logger.debug("【 Controller 】-【 LeaStudyCtrl 】-【 video 】-【 GET 】-【 COMPLETED 】");
		return "learn/course/video";
	}

}
